package com.ruoyi.project.jiaqin.newsclass.controller;

import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jiaqin.newsclass.domain.NewsClass;
import com.ruoyi.project.jiaqin.newsclass.service.INewsClassService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 新闻分类Controller
 *
 * @author admin
 * @date 2020-12-14
 */
@Controller
@RequestMapping("/jiaqin/newsclass")
public class NewsClassController extends BaseController
{
    private String prefix = "jiaqin/newsclass";

    @Autowired
    private INewsClassService newsClassService;

    @RequiresPermissions("jiaqin:newsclass:view")
    @GetMapping()
    public String newsclass()
    {
        return prefix + "/newsclass";
    }

    /**
     * 查询新闻分类列表
     */
    @RequiresPermissions("jiaqin:newsclass:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(NewsClass newsClass)
    {
        startPage();
        List<NewsClass> list = newsClassService.selectNewsClassList(newsClass);
        return getDataTable(list);
    }

    /**
     * 导出新闻分类列表
     */
    @RequiresPermissions("jiaqin:newsclass:export")
    @Log(title = "新闻分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(NewsClass newsClass)
    {
        List<NewsClass> list = newsClassService.selectNewsClassList(newsClass);
        ExcelUtil<NewsClass> util = new ExcelUtil<NewsClass>(NewsClass.class);
        return util.exportExcel(list, "newsclass");
    }

    /**
     * 新增新闻分类
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存新闻分类
     */
    @RequiresPermissions("jiaqin:newsclass:add")
    @Log(title = "新闻分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(NewsClass newsClass)
    {
        newsClass.setCreateAt(new Date());
        newsClass.setUpdateAt(new Date());
        return toAjax(newsClassService.insertNewsClass(newsClass));
    }

    /**
     * 修改新闻分类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        NewsClass newsClass = newsClassService.selectNewsClassById(id);
        mmap.put("newsClass", newsClass);
        return prefix + "/edit";
    }

    /**
     * 修改保存新闻分类
     */
    @RequiresPermissions("jiaqin:newsclass:edit")
    @Log(title = "新闻分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(NewsClass newsClass)
    {
        newsClass.setUpdateAt(new Date());
        return toAjax(newsClassService.updateNewsClass(newsClass));
    }

    /**
     * 删除新闻分类
     */
    @RequiresPermissions("jiaqin:newsclass:remove")
    @Log(title = "新闻分类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(newsClassService.deleteNewsClassByIds(ids));
    }
}
